﻿using Microsoft.Win32;
using System.IO;

namespace GetWallPagerFromBing
{
    class RegistryKeyOption
    {
        public static void SetStartAfterPowerOn()
        {
            string dir = Directory.GetCurrentDirectory();
            string exedir = dir + "\\GetWallPagerFromBing.exe";
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", false);

            foreach (var item in key.GetValueNames())
            {
                if (item == "BingImg")
                {
                    if (key.GetValue(item, null) == null)
                    {
                        try
                        {
                            key.DeleteValue(item);
                        }
                        catch (System.Exception e)
                        {
                            SimpleLog.GetLog(e.Message);
                        }

                    }
                    else
                    {
                        return;
                    }
                }
            }

            SetAutoRun("BingImg", exedir);
        }

        public static bool SetAutoRun(string keyName, string filePath)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
                runKey.SetValue(keyName, filePath);
                runKey.Close();
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
